﻿var rootURL='http://bds.hcmuaf.edu.vn';
function ajaxLoading(dom){
	dom.empty();
	dom.setStyle('min-height','100px');
	dom.setStyle('background', 'url(../images/loading2.gif) no-repeat center');
}
function ajaxLoading2(dom){
	dom.setOpacity(0.5);
	dom.setStyle('cursor','wait');
}
function ajaxUnloading(dom){
	dom.setStyle('height', 'auto');
	dom.setStyle('background', 'none');
}
function ajaxUnloading2(dom){
	dom.setOpacity(1);
	dom.setStyle('cursor','default');
}
function showObj(dom){
	dom.style.display='';
}
function isShowObj(dom){
	if(dom.style.display=='') return true;
	else return false;
}
function hideObj(dom){
	dom.style.display='none';
}
function isHideObj(dom){
	if(dom.style.display=='none') return true;
	else return false;
}


function requestBasic(urlIn,log,dataIn){
	ajaxLoading(log);
	new Request.HTML({
		url: urlIn,
		method: 'post',
		data: dataIn,
		update: log,
		onStart: function(){
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			ajaxUnloading(log);
		}
	}).send();
}
function requestAdv(urlIn,log,dataIn,successF){
	ajaxLoading(log);
	new Request.HTML({
		url: urlIn,
		method: 'post',
		data: dataIn,
		update: log,
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			ajaxUnloading(log);
			successF();
		}
	}).send();
}

function requestAdv2(urlIn,log,dataIn,successF){
	ajaxLoading2(log);
	new Request.HTML({
		url: urlIn,
		method: 'post',
		data: dataIn,
		update: log,
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			successF();
			ajaxUnloading2(log);
		}
	}).send();
}
function requestAdv3(urlIn,log){
	ajaxLoading2(log);
	new Request.HTML({
		url: urlIn,
		method: 'post',
		data: {},
		update: log,
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			ajaxUnloading2(log);
		}
	}).send();
}
function removeMyClass(rootElement,elementChirl,className){
	$(rootElement).getElements(elementChirl).each(function(it){
			if(it.hasClass(className)) it.removeClass(className);
		});
}
function addMyClass(rootElement,elementChirl,className){
	$(rootElement).getElements(elementChirl).each(function(it){
			if(!it.hasClass(className)) it.addClass(className);
		});
}

//goi ajax add cart tren button
function addCart(soLuong,idSanPham,dom){
	dom.setOpacity(0.5);
	beginMyBox('Đang thêm sản phẩm vào giỏ hàng! Xin chờ vài giây.');
	var urlAddCart=rootURL+'/cart/addgiohang';
	var data={idSanPham: idSanPham,soLuong: soLuong
			};
	var log=$('logRespont');
	var succeAddcartFun=function(){
			var info=log.get('html');
			updateInfoCart();
			dom.setOpacity(1);
			$('logThongBao').set('html',info);
			outMyBox.delay(3000);
		};
	requestAdv(urlAddCart,log,data,succeAddcartFun);
}

//funtion cai dat addcart
function addCartRegist(){
	
	var arrayAddCartButton=$$('.addCart');
	if(arrayAddCartButton!=null){
		arrayAddCartButton.addEvent('click', function(e){
				new Event(e).stop();
				var id=this.get('href');
				var soLuong=1;
				addCart(soLuong,id,this);
			});
		}
}
//update thong ngan cua gio hang o trang index
function updateInfoCart(){
	var urlAddCart=rootURL+'/cart/updategiohang';
	var data={};
	var log=$('logRespont');
	var succeAddcartFun=function(){
		$('logCartInfo').set('html',log.get('html'));
		};
	requestAdv(urlAddCart,log,data,succeAddcartFun);
}

//box thong bao ajax

var beginMyBox=function beginBox(text){
	var dom=$('logThongBao');
	dom.setStyle('visibility', 'visible');
	dom.setStyle('top', window.getScrollTop());
	dom.set('html',text);
	var effBegin= new Fx.Morph(dom, {
		duration: 1000
	});
	
	effBegin.start({
		height: [0, 25] ,
		opacity: [0, 0.9]       
	});
};
var outMyBox=function outBox(){
	var dom=$('logThongBao');
	var effBegin= new Fx.Morph(dom, {
		duration: 1000
	});
	
	effBegin.start({
		opacity: [1, 0],
		height: [25, 0] 
	});
};

//script dicrect

function dicrect(url){
	window.location.replace(url);
}

